import RPi.GPIO as GPIO
import argparse
import time
import sys

class led:
    def __init__(self,pinNum):
        self.pin = pinNum
        self.state = False # off
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pinNum, GPIO.OUT)
        GPIO.output(pinNum, False)

#    def __del__(self):
#        GPIO.cleanup()
#        
    def on(self):
        self.state = True # on
        GPIO.output(self.pin, True)

    def off(self):
        self.state = False # off
        GPIO.output(self.pin, False)

    def toggle(self):
        self.state = not self.state # change
        GPIO.output(self.pin, self.state)
          
def main(pin):

    my_led = led(pin)
    my_led.on()
    time.sleep(5) # on for 5 sec
    my_led.off()
      
      
    print "LED should be flashing, Ctrl C to stop"  
    while True:  
        try:  
            time.sleep(1) # on for 5 sec
            my_led.toggle()
        except KeyboardInterrupt:  
            print 'stopped'       # clean up GPIO on CTRL+C exit
            sys.exit()  

# use opt parse to get command line args
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='test the led object.')
    
    parser.add_argument('--pin', dest='pin', type=int, default=17,
                       help='pin number of the irq line, default = 17')
    
    args = parser.parse_args()
    print 'Pin is %d' % args.pin
    main(args.pin)
